<?php
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *
	 *	Autocredit
	 *	Copyright (C) 2012, Brent Flap <blog.cyberdevil.nl>
	 *
	 *	This file is part of Autocredit.
	 *
	 *	Autocredit is free software: you can redistribute it and/or modify
	 *	it under the terms of the GNU General Public License as published by
	 *	the Free Software Foundation, either version 3 of the License, or
	 *	any later version.
	 *
	 *	This program is distributed in the hope that it will be useful,
	 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
	 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 *	GNU General Public License for more details.
	 *
	 *	You should have received a copy of the GNU General Public License
	 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	class first_impressions extends Plugins{
		private static $form_patterns = array(
			"/<form(.*?)>/si",
			"/name=[\"']{1}(.*?)[\"']{1}/i",
			"/action=[\"']{1}(.*?)[\"']{1}/i",
			"/method=[\"']{1}(.*?)[\"']{1}/i",
			"/value=[\"']{1}(.*?)[\"']{1}/i",
			"/type=[\"']{1}(.*?)[\"']{1}/i",
			"/maxlength=[\"']{1}(.*?)[\"']{1}/i",
		);
		private static $plugin_name = "YEAH!";
		private static $cookie_name = "YEAH.cookie";
		private static $forms = array();
		function init(){
			message(self::$plugin_name . " [UNDER DEVELOPMENT!] loaded","COMMENT");
			return true;
		}
		function getForms($url){
			$curl_options = array(
				CURLOPT_COOKIEJAR	=> DATA_PATH.self::$cookie_name, // Write the cookie
				CURLOPT_RETURNTRANSFER	=> true,
				CURLOPT_FOLLOWLOCATION	=> 1
			);
			$content = self::getPage($url);
			$forms = explode("</form>",$content);
			foreach($forms as $f){
				$attributes = self::Parse(self::$form_patterns[0], $f, 1);
				if(strlen($attributes)>3){
					$attribute['name'] = self::Parse(self::$form_patterns[1], $attributes, 1);
					$attribute['action'] = self::Parse(self::$form_patterns[2], $attributes, 1);
					$attribute['method'] = self::Parse(self::$form_patterns[3], $attributes, 1);
					self::$forms[$attribute['name']]['action'] = $attribute['action'];
					self::$forms[$attribute['name']]['method'] = $attribute['method'];
					
					// Now the inputs (input, textarea and select)
					
					// Input (For radioboxes it will select the answer on the bottom, 'cause the names are the same and it will add the latest option on the last.)
					preg_match_all("/<input(.*?)>/si", $f, $inputs, PREG_SET_ORDER);
					foreach($inputs as $input){
						$input_attributes['name'] =  self::Parse(self::$form_patterns[1], $input[0], 1);
						$input_attributes['value'] =  self::Parse(self::$form_patterns[4], $input[0], 1);
						$input_attributes['type'] =  self::Parse(self::$form_patterns[5], $input[0], 1);
						$input_attributes['maxlength'] =  self::Parse(self::$form_patterns[6], $input[0], 1);
						self::$forms[$attribute['name']]['inputs'][$input_attributes['name']]['value'] = $input_attributes['value'];
						self::$forms[$attribute['name']]['inputs'][$input_attributes['name']]['type'] = $input_attributes['type'];
						self::$forms[$attribute['name']]['inputs'][$input_attributes['name']]['maxlength'] = $input_attributes['maxlength'];
					}
					
					// Select
					preg_match_all("/<select(.*?)<\/select>/si", $f, $selects, PREG_SET_ORDER);
					foreach($selects as $s){
						$select_name = self::Parse(self::$form_patterns[1], $s[0], 1);
						preg_match_all("/<option(.*?)<\/option>/si", $s[0], $options, PREG_SET_ORDER);
						$values = array();
						foreach($options as $opt){
							$value = self::Parse(self::$form_patterns[4], $opt[0], 1);
							if($value != ""){
								self::$forms[$attribute['name']]['selects'][$select_name][] = $value;
							}
						}
					}
				}
			}
		}
		function overruleValueByName($name,$value){
			foreach(self::$forms as $form_name => $a){
				//message($form_name." ".$a['action']." ".$a['method'],"NOTICE");
				foreach($a['inputs'] as $input_name => $atr){
					if($input_name == $name){
						self::$forms[$form_name]['inputs'][$input_name]['value'] = $value;
					}
				}
				if(isset($a['selects'])){
					foreach($a['selects'] as $select_name => $options){
						if($select_name == $name){
							self::$forms[$form_name]['selects'][$select_name]['value'] = $value;
						}
					}
				}
			}
		}
		function randomSelects(){
			foreach(self::$forms as $form_name => $a){
				if(isset($a['selects'])){
					foreach($a['selects'] as $select_name => $options){
						self::$forms[$form_name]['selects'][$select_name]['value'] = self::$forms[$form_name]['selects'][$select_name][rand(0,count(self::$forms[$form_name]['selects'][$select_name])-1)];
					}
				}
			}
		}
		function action(){
			$override = array(
				"jaar"			=> "1990",
				"email"			=> "baabaaa@hotmail.com",
				"privacy"		=> "ja",
				"privacy2"		=> "ja",
				"privacyWarchild"	=> "ja",
				"postcode1"		=> "3512",
				"postcode2"		=> "LR",
				"huisnummer"		=> "24",
				"telabbo"		=> "0592",
				"tel"			=> "583765",
				"mobiel"		=> "0652387634",
				"straat"		=> "Nieuwegracht",
				"plaats"		=> "Utrecht",
				"toevoeging"		=> "a",
				"voorletter"		=> "Bara",
				"tussenvoegsel"		=> "",
				"achternaam"		=> "Ufilzura",
				//
				"arrValues[prefix]"	=> "B",
				"arrValues[lastname]"	=> "Ufilzura",
				"arrValues[zipcode]"	=> "9302yt",
				"arrValues[housenumber]"=> "1",
				"arrValues[housenumberadd]"=> "a",
				"arrValues[emailaddress]"=> "dump@ababab.nl",
				"arrValues[telephonenumber]"=> "0582583753",
			);
			$url = "http://actie.detelefoongids.nl/em-132?id=224";
			self::getForms($url);
			self::randomSelects();
			foreach($override as $k => $v){
				self::overruleValueByName($k,$v);
			}
			$postfields = "";
			foreach(self::$forms as $form_name => $a){
				message($form_name." ".$a['action']." ".$a['method'],"NOTICE");
				foreach($a['inputs'] as $input_name => $atr){
					if($atr['value']=="" && $atr['type'] != "hidden"){
						$atr['value'] = "nee";
					}
					message("      INPUT: ".$input_name." ".$atr['type']." ".$atr['value'],"NOTICE");
					$postfields .= $input_name."=".$atr['value']."&";
				}
				if(isset($a['selects'])){
					foreach($a['selects'] as $select_name => $options){
						message("      SELECT: ".$select_name,"NOTICE");
						message("                   ".$options['value'],"NOTICE");
						$postfields .= $select_name."=".$options['value']."&";
						//foreach($options as $value){
						//	message("                   ".$value,"NOTICE");
						//}
					}
				}
			}
			$curl_options = array(
				CURLOPT_POST 		=> true,
				CURLOPT_POSTFIELDS	=> $postfields,
				CURLOPT_COOKIEFILE	=> DATA_PATH.self::$cookie_name, // Write the cookie
				CURLOPT_RETURNTRANSFER	=> true,
				CURLOPT_FOLLOWLOCATION	=> 1
			);
			$content = self::getPage("http://www.first-impressions.nl/enquetes/enqueteMB/postscripts/handleQuestion.php?id=132",$curl_options);
			//echo $content;
			//echo "\n\n\n".$postfields."\n";
			return true;
		}
	}
?>
